package com.cloud.phone.task

import android.os.Message
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPUtils
import com.cloud.phone.constants.Constants
import com.cloud.phone.task.bean.ProxyDataBean
import com.cloud.phone.task.bean.ProxyInfoBean
import com.cloud.phone.task.bean.ProxyVpnTaskBean
import com.cloud.phone.task.taskCongfig.TaskTypeConfig
import com.cloud.phone.websocket.WebsocketUtil
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import java.util.Date

/**
 * @auth: njb
 * @date: 2024/5/27 19:19
 * @desc: 描述
 */
object SendMsgUtils {
    private val TAG = "websocketSendMsg"
    private val format:SimpleDateFormat= SimpleDateFormat("HH:mm:ss");

    /**
     * 任务完成发送状态给服务器
     *
     * @param proxyInfoBean
     */
    fun sendMsgBean(proxyInfoBean: ProxyInfoBean) {
        try {
            val msgTaskBean = ProxyVpnTaskBean()
            msgTaskBean.let {
                it.taskType = Constants.VPN_TASK_TYPE
                it.type = Constants.TASK_TYPE
                it.code = 1003
                it.token= SPUtils.getInstance().getString("token")
                val infoBean = ProxyInfoBean()
                infoBean.apply {
                    if(proxyInfoBean.ip.isNotEmpty()){
                        this.ip = proxyInfoBean.ip
                    }
                    if(proxyInfoBean.port != 0){
                        this.port = proxyInfoBean.port
                    }
                    if(proxyInfoBean.account.isNotEmpty()){
                        this.account = proxyInfoBean.account
                    }
                    if(proxyInfoBean.password.isNotEmpty()){
                        this.password = proxyInfoBean.password
                    }
                    this.filePath = proxyInfoBean.filePath
                }
                val dataBean = ProxyDataBean()
                dataBean.apply {
                    this.taskType = Constants.VPN_TASK_TYPE
                    this.type = Constants.TASK_TYPE
                    this.json = GsonUtils.toJson(infoBean)
                    this.id = proxyInfoBean.id
                    this.status = if (Constants.TASK_STATUS) 2 else 3
                    it.data = this@apply
                }
                LogUtils.d(TAG, "===回传给后台的任务数据为===${GsonUtils.toJson(it)}")
                sendMsgToServer(GsonUtils.toJson(it))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    fun sendStopTaskMsgBean(id: String) {
        try {
            val msgTaskBean = ProxyVpnTaskBean()
            msgTaskBean.let {
                it.taskType = Constants.VPN_TASK_TYPE
                it.type = TaskTypeConfig.STOP_CURRENT_TASK
                it.code = 1003
                it.token= SPUtils.getInstance().getString("token")
                val dataBean = ProxyDataBean()
                dataBean.apply {
                    this.id = id
                    this.status = 2
                    this.taskType = Constants.VPN_TASK_TYPE
                    it.data = this@apply
                }
                sendMsgToServer(GsonUtils.toJson(it))
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun sendMsgToServer(msgTaskBean: String) {
        LogUtils.d(TAG, "===发送给后台的数据为===$msgTaskBean")
        var msg = Message.obtain()
        msg.obj = format.format(Date())+"-发送数据：\n$msgTaskBean"
        EventBus.getDefault().post(msg)
        WebsocketUtil.getWebsocketUtil().sendMsg(msgTaskBean)
        Constants.TASK_IS_RUNNING = false
        Constants.TASK_STATUS = false
    }
}